package com.yanqu.road.dao.impl.activity.dafuweng;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.DFWAttackRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWAttackRecordDaoImpl extends TempDataObjectDao<DFWAttackRecord> {

    public static String getDFWAttackRecordSql = "select * from t_c_activity_dfw_attack_record where activityId=? and groupId=? and monsterId>=?";

    public Map<Integer, Map<Long, DFWAttackRecord>> getDFWAttackRecordMap(int activityId, int groupId, int minBossId) {
        Map<Integer, Map<Long, DFWAttackRecord>> recordMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDFWAttackRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setInt(i++, minBossId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWAttackRecord dFWAttackRecord = parse(rs);
                if (!recordMap.containsKey(dFWAttackRecord.getMonsterId())) {
                    recordMap.put(dFWAttackRecord.getMonsterId(), new ConcurrentHashMap<>());
                }
                recordMap.get(dFWAttackRecord.getMonsterId()).put(dFWAttackRecord.getUserId(), dFWAttackRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return recordMap;
    }

    public static String updateDFWAttackRecordSql = "update t_c_activity_dfw_attack_record set userBaseInfo=?,damage=?,updateTime=? where activityId=? and groupId=? and monsterId=? and serverId=? and userId=?";

    @Override
    protected boolean updateData(DFWAttackRecord dFWAttackRecord) {
        dFWAttackRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWAttackRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWAttackRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWAttackRecord.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWAttackRecord.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getUserId()));
        if (getDb().executeNoneQuery(updateDFWAttackRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDFWAttackRecordSql = "insert into t_c_activity_dfw_attack_record(activityId,groupId,monsterId,serverId,userId,userBaseInfo,damage,updateTime) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DFWAttackRecord dFWAttackRecord) {
        dFWAttackRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWAttackRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWAttackRecord.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWAttackRecord.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWAttackRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWAttackRecord.getUpdateTime()));
        if (getDb().executeNoneQuery(insertDFWAttackRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DFWAttackRecord parseData(ResultSet rs) throws SQLException {
        DFWAttackRecord dFWAttackRecord = new DFWAttackRecord();
        dFWAttackRecord.setActivityId(rs.getInt("activityId"));
        dFWAttackRecord.setGroupId(rs.getInt("groupId"));
        dFWAttackRecord.setMonsterId(rs.getInt("monsterId"));
        dFWAttackRecord.setServerId(rs.getLong("serverId"));
        dFWAttackRecord.setUserId(rs.getLong("userId"));
        dFWAttackRecord.setUserBaseInfo(JSONObject.parseObject(rs.getString("userBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        dFWAttackRecord.setDamage(rs.getLong("damage"));
        dFWAttackRecord.setUpdateTime(rs.getLong("updateTime"));
        return dFWAttackRecord;
    }

}
